package com.uitls.anim.viewpager;

import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

/**
 * @author zhouzechao
 * 针对Viewpager放大缩小
 */
public class Transformer {

    private static final float MAX_SCALE = 1.2f;
    private static final float MIN_SCALE = 1.0f;//0.85f
    private static final float VIEW_SCALE = 0.7f;

    private static final float MAX_ALPHA = 1.0f;
    private static final float MIN_ALPHA = 0.6f;

    /**
     * 扩大缩小
     *
     * @return
     */
    public static ViewPager.PageTransformer zoomOutPage() {
        return new ZoomOutPageTransformer();
    }

    /**
     *
     * @return
     */
    public static ViewPager.PageTransformer alphaPage() {
        return new AlphaPageTransformer();
    }

    /**
     * 翻页
     *
     * @return
     */
    public static ViewPager.PageTransformer depthPage() {
        return new DepthPageTransformer();
    }

    private final static class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        @Override
        public void transformPage(View view, float position) {
            if (position < -1) {
                view.setScaleX(MIN_SCALE);
                view.setScaleY(MIN_SCALE);
            } else if (position <= 1) //a页滑动至b页 ； a页从 0.0 -1 ；b页从1 ~ 0.0
            { // [-1,1]
                float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);
            } else { // (1,+Infinity]
                view.setScaleX(MIN_SCALE);
                view.setScaleY(MIN_SCALE);

            }
        }
    }

    private final static class AlphaPageTransformer implements ViewPager.PageTransformer {
        @Override
        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();

            Log.e("TAG", view + " , " + position + "");

            if (position < -1) { // [-Infinity,-1)
                view.setAlpha(MIN_ALPHA);
                view.setScaleX(MIN_SCALE);
                view.setScaleY(MIN_SCALE);
            } else if (position <= 1) //a页滑动至b页 ； a页从 0.0 -1 ；b页从1 ~ 0.0
            { // [-1,1]
                float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);
//                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
//                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
//                if (position < 0) {
//                    view.setTranslationX(horzMargin - vertMargin / 2);
//                } else {
//                    view.setTranslationX(-horzMargin + vertMargin / 2);
//                }

                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);
                float alpha = MIN_ALPHA + (1 - Math.abs(position)) * (MAX_ALPHA - MIN_ALPHA);
                view.setAlpha(alpha);
            } else { // (1,+Infinity]
                view.setAlpha(MIN_ALPHA);
                view.setScaleX(MIN_SCALE);
                view.setScaleY(MIN_SCALE);
            }
        }
    }

    private final static class DepthPageTransformer implements ViewPager.PageTransformer {

        @Override
        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();

            if (position < -1) { // [-Infinity,-1)
                view.setAlpha(0);

            } else if (position <= 0) { // [-1,0]
                view.setAlpha(1 - Math.abs(position));
                view.setTranslationX(0);

                float scaleFactor = MIN_SCALE
                        + (MAX_SCALE - MIN_SCALE) * (1 - Math.abs(position));
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

            } else if (position <= 1) { // (0,1]
                view.setAlpha(1 - position);

                view.setTranslationX(pageWidth * -position);

                float scaleFactor = MIN_SCALE
                        + (MAX_SCALE - MIN_SCALE) * (1 - Math.abs(position));
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

            } else { // (1,+Infinity]
                view.setAlpha(0);
            }
        }
    }
}
